<?php // content="text/plain; charset=utf-8"
require_once APPPATH.'controllers/app.php';
class Managers extends App {
    public function __construct() {
        parent::__construct();
        $this->load->model('manager_model');
        $this->load->database();
    }
    function manager($pj_id){
        $data['title'] = "Phân công công việc";
        $data['css_files'] = array();
        $data['js_files'] = array();
        $data['sb'] = $this->_menu;
        $data['pj_id'] = $pj_id;
        if(is_admin()|| is_pm($pj_id))
            $data['managers'] = $this->manager_model->get_lever_1($pj_id);
        else {
           $data['managers'] = $this->manager_model->get_onwer($pj_id);
        }
        $data['content_title'] = "Phân công công việc";
        $this->load->view("manager/manager_view",$data);    
    }
    function add($pj_id){
        $this->loadadd($pj_id);
    }
    function loadadd($pj_id,$suc=null){
        $data['title'] = "Thêm phân công";
        $data['suc'] = $suc;
        $data['css_files'] = array(base_url().'assets/public/css/reveal.css');
        $data['js_files'] = array(base_url().'assets/public/js/jquery.reveal.js');
        if(is_admin()|| is_pm($pj_id)){
            
        }
        $data['pj_id'] = $pj_id;
        $data['sb'] = $this->_menu;
        $data['content_title'] = "Thêm phân công";
        $this->load->model('work_model');
        if(is_admin()||  is_pm($pj_id)){
             $data['works_free'] = $this->work_model->get_works_free_of_project($pj_id);
        }
        else {
             $pn_id = auth_id();
             $data['works_free'] = $this->work_model->get_child_works_of_pn($pj_id,$pn_id);
        }
        if(is_admin()||  is_pm($pj_id))
        $data['pns'] = $this->get_personnel_with_works_fill_option($pj_id);
        $this->load->view("manager/manager_add_view",$data);    
    }
    function addprocess($pj_id){
        // lay managerid cua cong viec cha cua cong viec dc chon
        $this->load->model('work_model');
        $workid = $this->input->post('work');
        $pn_id = $this->input->post('pn');
        $w_p = $this->work_model->get_parent($workid);
        $managerid = $this->manager_model->get_of_work($w_p);
        // kiem tra da dc phan nhom nhung chua phan cong
        $sql = "SELECT id FROM tblmanager WHERE projectid='$pj_id' AND personnelid = '$pn_id' AND workid='0' ";
        $arr_ = $this->db->query($sql)->result_array();
        if(count($arr_)>0){
           $arr = array(
            'personnelid'=>$this->input->post('pn'),
            'workid'=>$this->input->post('work'),
            'status'=>$this->input->post('status'),
            'roleid'=>'3',
            'parent'=>$managerid,
            'projectid'=>$pj_id
            );
           $this->db->where('id',$arr_[0]['id']);
           $this->db->update('tblmanager',$arr);
        }else {
            $arr = array(
            'personnelid'=>$this->input->post('pn'),
            'workid'=>$this->input->post('work'),
            'status'=>$this->input->post('status'),
            'roleid'=>'3',
            'parent'=>$managerid,
            'projectid'=>$pj_id
            );
            $this->db->insert('tblmanager',$arr);   
        }
        echo "<script>
                alert('Thêm thành công !');
                window.location.href='".  base_url()."managers/manager/$pj_id".
              "';</script>";
    }
    
    
    function get_personnel_with_works_fill_option($pj_id){
         $this->load->model('personnel_model');
		 $str = '';
         $str = (arrTooptions(
                        $this->personnel_model->get_for_assign_free($pj_id,'-1'),'name','id'));
         $arr = $this->personnel_model->get_for_assign_busy($pj_id);
         if($arr!=null){
            foreach ($arr as $a) {
                $des = str_replace('<br/>','1&#013',$a['des']);
                $str .= "<option value='".$a['personnelid']."' title='$des'>".$a['personnelname'].' (Đang bận)</option>';
            }
         }
		 return($str);
    }
    function check($pj_id,$pn_id,$workid){// Hàm so sánh công việc được phân với công việc đã được phân, trả về 1 ảnh
             $pj_id = $pj_id;
             $workid = $workid;
             $pn_id  = $pn_id;
             $this->load->model('work_model');
             // Load các công việc đã đươc phân vào ảnh
             $arr = $this->work_model->get_works_and_time_of_project_pn($pj_id,$pn_id);
             if(count($arr)>0){
                 require_once (APPPATH.'jpgraph/jpgraph.php');
                 require_once (APPPATH.'jpgraph/jpgraph_gantt.php');
                 $data = array();
                 $progress = array();
                 for($i=0;$i<count($arr);$i++){
                     if($arr[$i]['workid']!='0'){
                         $data[] = array($i,ACTYPE_NORMAL,'CV '.$arr[$i]['workname'].'(DA '.$arr[$i]['projectname'].')',$arr[$i]['beginonreality'],$arr[$i]['enddaywork'],'');
                         $progress[] = array($i,$arr[$i]['percent']/100);
                     }else {
                          $data[] = array($i,ACTYPE_GROUP,'DA '.$arr[$i]['projectname'],$arr[$i]['begindate'],$arr[$i]['enddate'],'');
                     }
                 }
                 $aw = $this->work_model->get_work($workid);
                 $date = new DateTime($aw[0]['beginonreality']);
                 $time = $aw[0]['timevalueonreality'];
                 $timeunit = $aw[0]['timeunitid'];
                     switch ($timeunit) {
                        case '1':
                            $date->modify("+$time hours");
                            break;
                        case '2':
                            $date->modify("+$time days");
                            break;
                        case '3':
                            $date->modify("+$time weeks");
                            break;
                        case '4':
                            $date->modify("+$time months");
                            break;
                        default:
                            break;
                  }
                 //Load công việc chuẩn bị được phân
                 $aw[0]['enddaywork'] = $date->format('Y-m-d');
                 $data[] = array($i,ACTYPE_NORMAL,'Duoc PC: '.$aw[0]['name'],$aw[0]['beginonreality'],$aw[0]['enddaywork'],'');
                 $constrains = array();
                 $graph = new GanttGraph();
                 $graph->title->Set("Cộng việc của ".$arr[0]['personnelname']);
                 // Setup scale
                 $graph->ShowHeaders(GANTT_HYEAR | GANTT_HMONTH | GANTT_HDAY | GANTT_HWEEK);
                 $graph->scale->week->SetStyle(WEEKSTYLE_FIRSTDAYWNBR);
                 // Add the specified activities
                 $graph->CreateSimple($data,$constrains,$progress);
                 // .. and stroke the graph
                 $graph->Stroke();
             }
       }
    function delete($id){
            $this->manager_model->delete($id);
            redirect($_SERVER['HTTP_REFERER']);
       }
    function ajax_is_free_with_project_pn($pj_id,$pn_id){
            $this->load->model('work_model');
        
            if($this->work_model->is_free_with_project_pn($pj_id,$pn_id)) {
                echo 'true';
            }else {
                echo 'false';
            }
    }
    function ajax_get_work_of_manager($manager_id){
        $this->load->model('work_model');
        $arr = $this->work_model->get_work_of_manager($manager_id);
        return json_encode(arrTooptions($arr,'name','id'));
    }
    function ajax_get_pn_of_boss_work($workid,$pj_id){
        $sql = "SELECT tblmanager.id FROM tblmanager
                WHERE tblmanager.workid = (SELECT parentid FROM tblworks WHERE `id`='$workid')";
        $query = $this->db->query($sql);
        if($query->num_rows>0){
            $arr = $query->result_array();
            $parent_maid = $arr[0]['id'];
            $sql2 = "SELECT tblmanager.personnelid as 'id',tblpersonnels.name,tblmanager.id as 'maid'
                    FROM tblmanager
                    JOIN tblpersonnels ON tblpersonnels.id = tblmanager.personnelid
                    WHERE tblmanager.parent = '$parent_maid'
                    ";
            $query2 = $this->db->query($sql2);
            if($query2->num_rows>0){
                $pns = $query2->result_array();
                $str = '';
                $this->load->model('personnel_model');
                // lay danh sach managerid con ma chua phan cong de loai tru cong viec dang lam
                $ma_without = $this->get_manager_without($parent_maid);
                foreach ($pns as $k => $v) {
                    if($this->personnel_model->is_busy($pj_id,$v['id'],null,$parent_maid)){
                        // print_r($this->personnel_model->get_work_busy($pj_id,$v['id'],null,$ma_without));
                        $pns[$k]['des'] =  $this->personnel_model->get_work_busy_string($pj_id,$v['id'],null,$ma_without);
                        if($pns[$k]['des']!='')
                        $str .= "<option value='".$v['id']."' title='".$pns[$k]['des']."'>".$v['name']." (Đang bận)</option>"; 
                        else $str .= "<option value='".$v['id']."'>". $v['name']."</option>"; 
                    }else {
                        $str .= "<option value='".$v['id']."'>". $v['name']."</option>"; 
                    }
                }
                echo json_encode($str);
            }else echo '';
        }else {
             echo '';
        }
    }
    function get_manager_without($parent_maid){
        $sql = "SELECT tblmanager.id FROM tblmanager  WHERE tblmanager.parent = '$parent_maid' AND workid='0'";
        $arr = $this->db->query($sql)->result_array();
        $str = '';
        foreach ($arr as $v) {
            $str .= $v['id'].'-';
        }
        if($str!=''){
            $str = substr($str,0,-1);
        }
        return $str;
    }
    function allow_assign($id){
           $data = array(
               'status'=>1
           );
           $this->db->where('id',$id);
           $this->db->update('tblmanager',$data);
           redirect($_SERVER['HTTP_REFERER']);
    }
	function deny_assign($id){
           $data = array(
               'status'=>0
           );
           $this->db->where('id',$id);
           $this->db->update('tblmanager',$data);
           redirect($_SERVER['HTTP_REFERER']);
    }
}
?>
